#include "TilePicker.h"


TilePicker::TilePicker(int x, int y, std::string prp , SDL_Surface* tiles)
{
	prop = prp;
	outer = new SDL_Rect();
	outer->x = x;
	outer->y = y;
	outer->h = 32;
	outer->w = 32;
	inner = new SDL_Rect();
	inner->h = 900;//Draw::NUM_IMAGES/Draw::TILESET_WIDTH;
	inner->w = 900;//Draw::TILESET_WIDTH*Draw::TILE_SIZE;
	inner->x = x+2;
	inner->y = y+2;
	isActive = false;
	value = "0";
    tileset = tiles;
	for(int i = 0 ; i<Draw::NUM_IMAGES; i++)
	{
		images[i].h = Draw::TILE_SIZE;
		images[i].w = Draw::TILE_SIZE;
		images[i].x = (i%Draw::TILESET_WIDTH)*Draw::TILE_SIZE;
		images[i].y = (i/Draw::TILESET_WIDTH)*Draw::TILE_SIZE;
	}
	selected =20;
}


TilePicker::~TilePicker(void)
{
}

void TilePicker:: onClick()
{
	isActive=!isActive;
}
	void TilePicker::onClickOut()
	{
		isActive = false;
	}
	void TilePicker::drawElement(SDL_Surface* screen)
	{
		if(isActive)
		{
			Draw::apply_surface(outer->x,outer->y , tileset , screen );	
		}else
			{
				Draw::apply_surface(outer->x,outer->y , tileset , screen , &images[selected]);
		}
		char * buf = "";
		std::stringstream out;
out << selected;
value = out.str();
		
	}
	void TilePicker::handleInput(SDL_Event event)
	{
		int x=0;
		int y=0;
		if( event.type == SDL_MOUSEBUTTONDOWN )
		{
		
			//If the left mouse button was pressed
			if( event.button.button == SDL_BUTTON_LEFT )
			{
				//Get the mouse offsets
				
				x = event.button.x;
				y = event.button.y;
				
				if(x>=inner->x && x<=(inner->w + inner->x))
	{
		if(y>=inner->y && y<=(inner->h + inner->y))
		{
			x-=this->outer->x;
				y-=this->outer->y;
					selected = (x/Draw::TILE_SIZE) + ((y/Draw::TILE_SIZE)*Draw::TILESET_WIDTH);

		}
	}
					//need to preload information about game objects. or something.
					//right now we're like one of those kids with a pretend cell phone.
					//just drawing fake maps that don't do shit.

				
	}
		}
	}